#include "Store_menu.h"
#include <algorithm>
using namespace std;
using namespace Store;


bool SortProdByPrice(Product a, Product b){
        return a.getProdPrice() > b.getProdPrice();
}


DisplayNJG::DisplayNJG(){ }

string DisplayNJG::display()
{
		//product max display format
	extern Store_menu m;
	vector<Product> initial= m.prods;
	vector<Product> final;
	vector<Cart_item> items=m.cart_items;
	cout << initial.size();
	cout << items.size();
	for(int i=0; i<initial.size(); ++i)
	{
		double sales_total = 0;
		double prod_price = initial[i].getProdPrice();
		//total up all product sales for this product
		for (int j = 0; j < items.size(); ++j)
			//select a cart item for this product
			if(items[j].getProdId() == initial[i].getProdId())
				//add the sale of this item to the total
				sales_total += prod_price * items[j].getProdQty();	
		final.push_back(Product(initial[i].getProdId(),initial[i].getCatId(),initial[i].getProdName(),sales_total));
	}

	sort (final.begin(), final.end(),SortProdByPrice);
	string r;
	if(0==final.size()) r= "No Product Data Loaded\n";
	else{
		r+="Product ID\tCategory ID\tProduct Name\tTotal Sales\n\n";
		for(int i(0);i<5;++i)
		{
			r+= final[i].display();
			r+='\n';
		}
	}
	return r;
}